<template>
  <el-form
    ref="loginForm"
    :model="loginForm"
    :rules="rules"
    label-width="120px"
    class="loginForm sign-in-form"
    @keyup.enter.native="handleLogin"
  >
    <el-form-item label="用户名" prop="name">
      <el-input v-model="loginForm.name" placeholder="输入用户名" />
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
        v-model="loginForm.password"
        type="password"
        placeholder="输入密码"
        show-password
      />
    </el-form-item>
    <el-form-item>
      <!-- 提交前进行校验 -->
      <el-button
        @click.native.prevent="handleLogin"
        type="primary"
        class="submit-btn"
        >登录</el-button
      >
    </el-form-item>
    <!-- 找回密码 -->
    <div class="tiparea">
      <p>忘记密码？ <button>立即找回</button></p>
    </div>
  </el-form>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import { UserModule } from '@/store/modules/user'
import { ElForm } from 'element-ui/types/form'

@Component({})
export default class LoginForm extends Vue {
  private loading = false
  private redirect?: string
  private rules = {
    name: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 3, max: 11, message: '长度应为3到11个字符', trigger: 'blur' },
    ],
    password: [
      {
        required: true,
        message: '密码不能为空',
        trigger: 'blur',
      },
      { min: 6, max: 12, message: '密码长度应为6到12个字符', trigger: 'blur' },
    ],
  }
  private loginForm = {
    name: '',
    password: '',
  } as {
    name: String
    password: String
  }
  private handleLogin() {
    ;(this.$refs.loginForm as ElForm).validate(async (valid: boolean) => {
      if (valid) {
        this.loading = true
        await UserModule.Login(this.loginForm as any)
          .then((res: any) => {
            if (String(res.code) === '1') {
              this.$router.push('/dashboard')
            } else {
              this.loading = false
            }
          })
          .catch(() => {
            this.loading = false
          })
      } else {
        return false
      }
    })
  }
}
</script>
 <style scoped>
/* form */
.loginForm {
  margin-top: 350px;
  background-color: #fff;
  padding: 20px 40px 20px 20px;
  border-radius: 5px;
  box-shadow: 0px 5px 10px #cccc;
  border-radius: 30px !important;
}

.submit-btn {
  width: 100%;
}
.tiparea {
  text-align: right;
  font-size: 12px;
  color: #333;
}
.tiparea p button {
  color: #3590f0;
  background-color: #fff;
  border: inherit;
  cursor: pointer;
}
.el-button--primary {
  background: #3590f0;
}
</style>